module.exports = app => {
    const express = require('express')
    const router = express.Router()
    //导入模型
    const Category = require('../model/category')
    const User = require('../model/user')
    const ArticleTitle = require('../model/articleTitle')
    const Article = require('../model/article')
    const jwt = require('jsonwebtoken');

    //用户
    router.post('/user', async (req, res) => {
        console.log(req.body)
        const model = await User.create(req.body)
        res.send({
            code: 200,
            result: null,
            msg: '创建成功'
        })
    })

    router.get('/user/:id', async (req, res) => {
        const model = await User.findOne(req.params.id)
        res.send({
            code: 200,
            result: model,
            msg: '查询成功'
        })
    })
    /*登录
     *查询是否有账号，有就查询是否密码正确，正确返回token，不正确返回密码错误
     *没有账号就返回账号不正确
     */
    router.post('/login', async (req, res) => {
        const {
            username,
            password
        } = req.body
        const user = await User.findOne({
            username: username
        })
        if (user) {
            if (password == user.password) {
                const token = jwt.sign({
                    id: user.id
                }, 'sadsok5kas2la4sas40aasa')
                res.send({
                    code: 200,
                    result: token,
                    msg: '登录成功'
                })
            } else {
                return res.status(421).send({
                    msg: '请输入正确的密码'
                })
            }
        } else {
            return res.status(420).send({
                msg: '账号不存在'
            })
        }
    })


    //注册
    router.post('/register', async (req, res) => {
        const {
            username,
            password,
            email
        } = req.body
        const model = await User.findOne({
            email: email
        })
        console.log(model)
        if (model) {
            return res.status(420).send({
                msg: '账号不存在'
            })
        } else {
            User.create(req.body)
            res.send('注册成功')
        }
    })

    //分类
    router.get('/category/list', async (req, res) => {
        const model = await Category.find()
        res.send({
            code: 200,
            result: model,
            msg: ''
        })
    })
    router.get('/category/info', async (req, res) => {
        const model = await Category.findOne({_id:req.query.id})
        res.send({
            code: 200,
            result: model,
            msg: ''
        })
    })

    router.post('/category/add', async (req, res) => {
        const model = await Category.create(req.body)
        res.send({
            code: 200,
            result: model,
            msg: '创建成功'
        })
    })

// 根据 id 去修改数据展示
    router.post("/category/update",async(req,res)=> {
        const model = await Category.findById(req.body.id) // 先通过 id 找到相关的数据展示
        model.name = req.body.name // 直接将发送过来的数据 进行保存
        await model.save() // 一定要记得保存啊
        res.send({
            code: 200,
            result: model,
            msg: '修改成功'
        })

    })
        router.post('/category/delete', (req, res) => {
        console.log(req.body)
        Category.findByIdAndDelete(req.body.id, req.body, (err, user) => {
            if (err) {
                res.send({
                    code: 200,
                    result: model,
                    msg: err
                })
            } else {
                res.send({
                    code: 200,
                    result: null,
                    msg: '删除成功'
                })
            }
        })
    })


    //文章分类列表

    router.get('/article/title', async (req, res) => {
        const model =  await ArticleTitle.find()
        res.send({
            code: 200,
            result:model,
            msg: ''
        })
    })
    router.post('/article/title/list', async (req, res) => {
        let currentPage = parseInt(req.body.currentPage);
        let pageSize = parseInt(req.body.pageSize);
        let skip = (currentPage-1)*pageSize
        const model  = await ArticleTitle.find().skip(skip).limit(pageSize)
        const total = await ArticleTitle.find().count()
        res.send({
            code: 200,
            result:{
                list:model,
                total:total
            },
            msg: ''
        })
    })
    router.post('/article/title/add', async (req, res) => {
        const model = await ArticleTitle.create(req.body)
        res.send({
            code: 200,
            result: model,
            msg: '创建成功'
        })
    })


    //文章
    router.post('/article/add', async (req, res) => {
        const model = await Article.create(req.body)
        res.send({
            code: 200,
            result: model,
            msg: '创建成功'
        })
    })

    //列表分页
    router.post('/article/list', async (req, res) => {
        let currentPage = parseInt(req.body.currentPage); //当前第几页
        let pageSize = parseInt(req.body.pageSize);    //一页多少条
        let skip = (currentPage-1)*pageSize //跳过数
        const model  = await Article.find().populate('articleCategory').skip(skip).limit(pageSize)
        const total = await Article.find().count()
        // populate后面加关联字段的名字
        // count() 查询当前数组的长度
        // limit 限制当前数组的长度
        res.send({
            code:200,
            result:{
                list:model,
                total:total
            }
        })
    })

    router.post('/article/list/delete', async (req, res) => {
        const model = await Article.findOneAndDelete(req.body.id, req.body)
        res.send({
            code: 200,
            result: model,
            msg: "删除成功"
        })
    })

    app.use(router)
}